package multi_thread;

/**
 * @description:
 * @author: walt-zhong
 * @createDate: 2021/3/8
 */
public class PrintABC2 {

    public class print implements Runnable{
        private final Object frontLock;
        private final Object thisLock;
        private static final int printCount = 10;
        private final String printStr;

        public print(Object frontLock,Object thisLock,String printStr){
            this.frontLock = frontLock;
            this.thisLock = thisLock;
            this.printStr = printStr;
        }

        @Override
        public void run() {
            for(int i=0;i<printCount;i++){
                synchronized (frontLock){
                    synchronized (thisLock){
                        System.out.print(printStr);
                        thisLock.notify();
                    }
                    if(i<printCount-1){
                        try {
                            frontLock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
    }

    public void test(){
        Object objectA = new Object();
        Object objectB = new Object();
        Object objectC = new Object();

        new Thread(new print(objectC,objectA,"A")).start();
        new Thread(new print(objectA,objectB,"B")).start();
        new Thread(new print(objectB,objectC,"C")).start();

    }

    public static void main(String [] args){
        new PrintABC2().test();
    }
}
